<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Li ShiWang</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta property="og:type" content="website">
<meta property="og:title" content="Li ShiWang">
<meta property="og:url" content="https://jggnice.github.io/page/2/index.html">
<meta property="og:site_name" content="Li ShiWang">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="Li ShiWang">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Li ShiWang" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Li ShiWang</a>
      </h1>
      
        <h2 id="subtitle-wrap">
          <a href="/" id="subtitle">个人站</a>
        </h2>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://jggnice.github.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main">
  
    <article id="post-2016-10-18-java-code" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/posts/14858.html" class="article-date">
  <time class="dt-published" datetime="2016-10-17T23:00:25.000Z" itemprop="datePublished">2016-10-18</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/posts/14858.html">Java code at 2016-10-18</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 猜拳游戏</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 1.根据运行结果分析业务</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 2.通过控制台进入界面菜单</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 3.可选择对战角色</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 4.使用随机数完成人机猜拳效果</span></span><br><span class="line"><span class="comment"> * &lt;p&gt;</span></span><br><span class="line"><span class="comment"> * 5.统计每次猜拳的分数,游戏结束,显示最终结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">CaiQuan</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">	final <span class="keyword">static</span> int WIN = <span class="number">1</span>;</span><br><span class="line">	final <span class="keyword">static</span> int FAIL = <span class="number">2</span>;</span><br><span class="line">	final <span class="keyword">static</span> int TIE = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * player 记录用户输入数字</span></span><br><span class="line"><span class="comment">	 * &lt;p&gt;</span></span><br><span class="line"><span class="comment">	 * computer 记录随机数</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	int player, computer;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 统计对战次数</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	int count = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * playerscore 记录用户分数</span></span><br><span class="line"><span class="comment">	 * &lt;p&gt;</span></span><br><span class="line"><span class="comment">	 * computerscore 记录电脑分数</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	int playerscore = <span class="number">0</span>;</span><br><span class="line">	int computerscore = <span class="number">0</span>;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * username 记录用户名</span></span><br><span class="line"><span class="comment">	 * &lt;p&gt;</span></span><br><span class="line"><span class="comment">	 * cname 记录电脑名</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="built_in">String</span> cname, username;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 电脑名数组</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="built_in">String</span>[] computers =</span><br><span class="line">	&#123; <span class="string">&quot;刘备&quot;</span>, <span class="string">&quot;孙权&quot;</span>, <span class="string">&quot;曹操&quot;</span> &#125;;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 游戏三种情况</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="built_in">String</span>[] words =</span><br><span class="line">	&#123; <span class="string">&quot;剪刀&quot;</span>, <span class="string">&quot;石头&quot;</span>, <span class="string">&quot;布&quot;</span> &#125;;</span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 结果数组</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="built_in">String</span>[] info =</span><br><span class="line">	&#123; <span class="string">&quot;和局,真衰!等着瞧吧!&quot;</span>, <span class="string">&quot;恭喜,你赢了!&quot;</span>, <span class="string">&quot;哈哈!,你输了!&quot;</span> &#125;;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * 用来打印最终结果</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	<span class="keyword">void</span> <span class="function"><span class="title">printResult</span>(<span class="params"></span>)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println(<span class="string">&quot;-----------------------&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;对战次数:&quot;</span> + count);</span><br><span class="line">		System.out.println(<span class="string">&quot;姓名\t得分&quot;</span>);</span><br><span class="line">		System.out.println(username + <span class="string">&quot;\t&quot;</span> + playerscore);</span><br><span class="line">		System.out.println(cname + <span class="string">&quot;\t&quot;</span> + computerscore);</span><br><span class="line">		<span class="keyword">if</span> (playerscore &gt; computerscore)</span><br><span class="line">			System.out.println(<span class="string">&quot;结果: 恭喜恭喜,你的分数更高!&quot;</span>);</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span> (playerscore &lt; computerscore)</span><br><span class="line">			System.out.println(<span class="string">&quot;结果: 你的分数更低&quot;</span>);</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			System.out.println(<span class="string">&quot;结果:难分难解,平局!&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * 用来判断胜负</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	int <span class="function"><span class="title">getResult</span>(<span class="params"></span>)</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span> ((player + <span class="number">1</span>) % <span class="number">3</span> == computer)</span><br><span class="line">		&#123;</span><br><span class="line">			computerscore++;</span><br><span class="line">			<span class="keyword">return</span> FAIL;</span><br><span class="line">		&#125; <span class="keyword">else</span> <span class="keyword">if</span> (player == computer)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> TIE;</span><br><span class="line">		&#125; <span class="keyword">else</span></span><br><span class="line">		&#123;</span><br><span class="line">			playerscore++;</span><br><span class="line">			<span class="keyword">return</span> WIN;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * 用来初始化菜单</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	public <span class="keyword">void</span> <span class="function"><span class="title">initGame</span>(<span class="params"></span>)</span></span><br><span class="line">	&#123;</span><br><span class="line">		System.out.println();</span><br><span class="line">		System.out.println(<span class="string">&quot;******************欢迎进入游戏世界*********************&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;\t\t***********&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;\t\t**猜拳开始**&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;\t\t***********&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;出拳规则:1.剪刀2.石头3.布&quot;</span>);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/**</span></span><br><span class="line"><span class="comment">	 * </span></span><br><span class="line"><span class="comment">	 * 开始一个游戏</span></span><br><span class="line"><span class="comment">	 * */</span></span><br><span class="line">	public <span class="keyword">void</span> <span class="function"><span class="title">startGame</span>(<span class="params"></span>)</span></span><br><span class="line">	&#123;</span><br><span class="line">		initGame();</span><br><span class="line">		Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">		System.out.println(<span class="string">&quot;请选择对方角色:(1:刘备2.孙权3.曹操)&quot;</span>);</span><br><span class="line">		cname = computers[scanner.nextInt() - <span class="number">1</span>];</span><br><span class="line">		System.out.println(<span class="string">&quot;请输入你的姓名:&quot;</span>);</span><br><span class="line">		username = scanner.next();</span><br><span class="line">		System.out.println(username + <span class="string">&quot; VS &quot;</span> + cname + <span class="string">&quot; 对战&quot;</span>);</span><br><span class="line">		System.out.println(<span class="string">&quot;要开始吗?(y/n)&quot;</span>);</span><br><span class="line">		<span class="keyword">if</span> (<span class="string">&quot;n&quot;</span>.equals(scanner.next()))</span><br><span class="line">		&#123;</span><br><span class="line">			printResult();</span><br><span class="line">			System.exit(<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">while</span> (<span class="literal">true</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			count++;</span><br><span class="line">			System.out.println(<span class="string">&quot;请出拳:1.剪刀2.石头3.布&quot;</span>);</span><br><span class="line">			player = scanner.nextInt() - <span class="number">1</span>;</span><br><span class="line">			computer = (int) (<span class="built_in">Math</span>.random() * <span class="number">3</span>);</span><br><span class="line">			System.out.println(<span class="string">&quot;你出拳:&quot;</span> + words[player]);</span><br><span class="line">			System.out.println(cname + <span class="string">&quot;出拳:&quot;</span> + words[computer]);</span><br><span class="line">			System.out.println(<span class="string">&quot;结果:&quot;</span> + info[getResult()]);</span><br><span class="line">			System.out.println(<span class="string">&quot;\n是否进入下一轮:(y/n)&quot;</span>);</span><br><span class="line">			<span class="keyword">if</span> (<span class="string">&quot;n&quot;</span>.equals(scanner.next()))</span><br><span class="line">			&#123;</span><br><span class="line">				printResult();</span><br><span class="line">				System.exit(<span class="number">0</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	public <span class="keyword">static</span> <span class="keyword">void</span> <span class="function"><span class="title">main</span>(<span class="params"><span class="built_in">String</span>[] args</span>)</span></span><br><span class="line">	&#123;</span><br><span class="line"></span><br><span class="line">		CaiQuan game = <span class="keyword">new</span> CaiQuan();</span><br><span class="line">		game.startGame();</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://jggnice.github.io/posts/14858.html" data-id="ckuc28gm80005vcqe5aa6gmjy" data-title="Java code at 2016-10-18" class="article-share-link">分享</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java-code/" rel="tag">java code</a></li></ul>

    </footer>
  </div>
  
</article>



  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/">&laquo; 上一页</a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><a class="page-number" href="/page/4/">4</a><span class="space">&hellip;</span><a class="page-number" href="/page/6/">6</a><a class="extend next" rel="next" href="/page/3/">下一页 &raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/java-code/" rel="tag">java code</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E3%80%8ALaTeX%E3%80%8B/" rel="tag">《LaTeX》</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E3%80%8AWindows%E3%80%8B/" rel="tag">《Windows》</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E3%80%8Agit%E3%80%8B/" rel="tag">《git》</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/%E3%80%8Avim%E3%80%8B/" rel="tag">《vim》</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/java-code/" style="font-size: 10px;">java code</a> <a href="/tags/%E3%80%8ALaTeX%E3%80%8B/" style="font-size: 10px;">《LaTeX》</a> <a href="/tags/%E3%80%8AWindows%E3%80%8B/" style="font-size: 10px;">《Windows》</a> <a href="/tags/%E3%80%8Agit%E3%80%8B/" style="font-size: 10px;">《git》</a> <a href="/tags/%E3%80%8Avim%E3%80%8B/" style="font-size: 10px;">《vim》</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/10/">十月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/posts/16107.html">Hello World</a>
          </li>
        
          <li>
            <a href="/posts/14858.html">Java code at 2016-10-18</a>
          </li>
        
          <li>
            <a href="/posts/27525.html">从零开始LaTeX快速入门</a>
          </li>
        
          <li>
            <a href="/posts/36939.html">学会使用vim</a>
          </li>
        
          <li>
            <a href="/posts/17347.html">打造一个简洁高效的Windows环境</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Li ShiWang<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>